﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GuiLibrary.layouts;
using Microsoft.Xna.Framework;
using GameEngine.Scenes;
using Microsoft.Xna.Framework.Graphics;

namespace GuiLibrary.Components
{
    public class Panel: Component
    {
        //public Vector2 size { get; set; } 
        public Background background { get; set; }
        public Layout layout { get; set; }
        public List<Component> components { get; set; }
        
        public Panel(Component parent, Vector2 relPosition, Background background, Layout layout, List<Component> components) :
            base(parent, relPosition)
        {
            this.layout = layout;
            this.components = components;
            this.background = background;
            if (background != null) this.background.position = getPosition();
        }
        public Panel(Component parent, Vector2 relPosition, Background background) :
            this(parent, relPosition, background, null, new List<Component>()) { }
        public Panel(Vector2 relPosition, Background background) :
            this(null, relPosition, background, null, new List<Component>()) { }
        public Panel(Component parent) :
            this(parent, new Vector2(), null, null, new List<Component>()) { }
        public Panel():
            this(null, new Vector2(), null, null, new List<Component>()) { }

        public override void draw(SpriteBatch sb)
        {
            if(background!=null)background.draw(sb);
            foreach (Component component in components) 
            {
                if(component != null) component.draw(sb);
            }
        }
        public override void update(int dTime) 
        {
            layout.update(dTime);
        }
        public override void moveCursor(Layout.Cursor cursor) 
        {
            layout.moveCursor(cursor);
            //foreach (Component component in components) moved to layout
            //    component.moveCursor(cursor);
        }
        public bool canMove(Layout.Cursor cursor) 
        {
            return layout.canMove(cursor);
        }
        public override Vector2 getSize()
        {
            if (parent == null)
            {
                return background.getCurrentAnimation().getFrameSize();
            }
            else 
            {
                return ((Panel)parent).layout.getComponentSize(this);
            }
        }
        public override void refresh()
        {
            layout.refresh();
            base.refresh();
        }
        public void eraseComponent(Component component) 
        {
            if (components.Contains(component)) 
            {
                int index = components.IndexOf(component);
                layout.eraseComponent(index);
                components.Remove(component);
                component.parent = null;
                refresh();
            }
        }
    }
}
